package UI;

import controller.book.Book_add;
import controller.book.Book_del;
import controller.client.Client_add;
import controller.client.Client_del;
import controller.goods.Goods_add;
import controller.goods.Goods_del;
import controller.tongji.UIcash;
import controller.worker.Worker_add;
import controller.worker.Worker_del;
import entity.Client;
import entity.Goods;
import entity.User;
import util.JdbcDeal;
import controller.Admin.Admin_update;
import controller.worker.Worker_update;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.sql.ResultSet;
import java.sql.SQLException;

public class MainFrame extends JFrame implements ActionListener {

    private User user;
    private JMenuBar mb = new JMenuBar();

    private JMenu Personnelmanagement = new JMenu("人员管理");
    private JMenu Itemmanagement = new JMenu("物品管理");
    private JMenu Personal = new JMenu("个人");

    private JMenu JMworker = new JMenu("员工");
    private JMenuItem JMworker_add = new JMenuItem("员工添加");
    private JMenuItem JMworker_information = new JMenuItem("员工信息");
    private JMenuItem JMworker_del = new JMenuItem("删除员工");

    private JMenu JMclient = new JMenu("客户");
    private JMenuItem JMclient_add = new JMenuItem("客户添加");
    private JMenuItem JMclient_information = new JMenuItem("客户信息");
    private JMenuItem JMclient_del = new JMenuItem("删除客户");

    private JMenu JMgoods = new JMenu("货物");
    private JMenuItem JMgoods_add = new JMenuItem("货物添加");
    private JMenuItem JMgoods_information = new JMenuItem("货物信息");
    private JMenuItem JMgoods_del = new JMenuItem("删除货物");

    private JMenu JMbook = new JMenu("订单");
    private JMenuItem JMbook_add = new JMenuItem("订单添加");
    private JMenuItem JMbook_information = new JMenuItem("订单信息");
    private JMenuItem JMbook_del = new JMenuItem("删除订单");

    private JMenuItem Personal_information = new JMenuItem("个人信息");
    private JMenuItem JMsort = new JMenuItem("货物类别");
    private JMenuItem JMcash = new JMenuItem("销售统计");
    private JMenuItem JMquit = new JMenuItem("注销");

    private JPanel p;

    private void Perfect_information(){
        if(!user.isFlag()){
            try {
                JdbcDeal jdbcDeal = new JdbcDeal();
                String sql = "SELECT * FROM worker WHERE Wno = '" + this.user.getWorkNo() + "'";
                ResultSet resultSet = jdbcDeal.select(sql);
                resultSet.first();
                user.setName(resultSet.getString("Wname"));
                user.setSex(resultSet.getString("Wsex"));
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }else {
            return;
        }
    }

    public MainFrame(User user){
        super("销售管理系统");
//        this.setSize(1600,900);
//        this.setResizable(false);

        ImageIcon icon = new ImageIcon("img/Main.jpg");
        //将图片放入label中
        JLabel label = new JLabel(icon);
        //设置label的大小
        label.setBounds(0,0,icon.getIconWidth(),icon.getIconHeight());
        getLayeredPane().add(label,new Integer(Integer.MIN_VALUE));
        this.setSize(icon.getIconWidth(),icon.getIconHeight());
        JPanel panelTop = (JPanel)this.getContentPane();
        panelTop.setOpaque(false);


        this.user = user;
        Perfect_information();

        //设置当前窗口的菜单条
        this.setJMenuBar(mb);

        Personal.add(Personal_information);
        Personal.add(JMquit);

        Personnelmanagement.add(JMworker);
        JMworker.add(JMworker_add);
        JMworker.add(JMworker_information);
        JMworker.add(JMworker_del);

        Personnelmanagement.add(JMclient);
        JMclient.add(JMclient_add);
        JMclient.add(JMclient_information);
        JMclient.add(JMclient_del);

        Itemmanagement.add(JMgoods);
        JMgoods.add(JMgoods_add);
        JMgoods.add(JMgoods_information);
        JMgoods.add(JMgoods_del);

        Itemmanagement.add(JMbook);
        JMbook.add(JMbook_add);
        JMbook.add(JMbook_information);
        JMbook.add(JMbook_del);

        Itemmanagement.add(JMsort);
        Itemmanagement.add(JMcash);
        mb.add(Itemmanagement);
        mb.add(Personnelmanagement);
        mb.add(Personal);


        //注册窃听器
        Personal_information.addActionListener(this);
        JMgoods.addActionListener(this);
        JMbook.addActionListener(this);

        JMworker_add.addActionListener(this);
        JMworker_information.addActionListener(this);
        JMworker_del.addActionListener(this);

        JMclient_add.addActionListener(this);
        JMclient_information.addActionListener(this);
        JMclient_del.addActionListener(this);

        JMgoods_add.addActionListener(this);
        JMgoods_information.addActionListener(this);
        JMgoods_del.addActionListener(this);

        JMbook_add.addActionListener(this);
        JMbook_information.addActionListener(this);
        JMbook_del.addActionListener(this);

        JMsort.addActionListener(this);
        JMcash.addActionListener(this);
        JMquit.addActionListener(this);

        /* 使用窗口适配器为窗口添加监听,实现关闭操作 */
        this.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                close();  //调用close()方法来关闭
            }
        });

        //		窗口将置于屏幕的中央
        this.setLocationRelativeTo(null);
        this.setVisible(true);
    }

    public void close() {    //关闭窗口的操作
        //	使用dispose()方法关闭窗体会释放该窗体的占用的部分资源，不过呢不是全部的
        this.dispose();
        //此语句关闭窗口程序会结束
        System.exit(0);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        String str = e.getActionCommand();

        if(str.equals("货物信息")){
            if(p != null){
                p.setVisible(false);
                this.remove(p);
            }
            p = new UIgoods(user);
            p.setOpaque(false);
            this.add(p);
            validate();
            return;
        }else if(str.equals("货物添加")){
            if(p != null){
                p.setVisible(false);
                this.remove(p);
            }
            Goods_add goods_add = new Goods_add();
            validate();
            return;
        }
        else if(str.equals("删除货物")){
            if(p != null){
                p.setVisible(false);
                this.remove(p);
            }
            new Goods_del();
            validate();
            return;
        }
        else if(str.equals("订单添加")){
            if(p != null){
                p.setVisible(false);
                this.remove(p);
            }
            Book_add book_add = new Book_add(user);
            validate();
            return;
        } else if(str.equals("订单信息")){
            if(p != null){
                p.setVisible(false);
                this.remove(p);
            }
            p = new UIbook(user);
            p.setOpaque(false);
            this.add(p);
            validate();
            return;
        }
        else if(str.equals("删除订单")){
            if(p != null){
                p.setVisible(false);
                this.remove(p);
            }
            new Book_del(user);
            validate();
            return;
        }

        else if(str.equals("员工添加")){
            if(p != null){
                p.setVisible(false);
                this.remove(p);
            }
            Worker_add worker_add = new Worker_add();
            validate();
            return;
        }else if(str.equals("员工信息")){
            if(p != null){
                p.setVisible(false);
                this.remove(p);
            }
            p = new UIworker(user);
            p.setOpaque(false);
            this.add(p);
            validate();
            return;
        }
        else if(str.equals("删除员工")){
            if(p != null){
                p.setVisible(false);
                this.remove(p);
            }
            new Worker_del(user);
            validate();
            return;
        }
        else if(str.equals("客户添加")) {
            if(p != null){
                p.setVisible(false);
                this.remove(p);
            }
            Client_add client_add = new Client_add();
            validate();
            return;
        }
        else if(str.equals("客户信息")){
            if(p != null){
                p.setVisible(false);
                this.remove(p);
            }
            p = new UIclient(user);
            p.setOpaque(false);
            this.add(p);
            validate();
            return;
        }
        else if(str.equals("删除客户")){
            if(p != null){
                p.setVisible(false);
                this.remove(p);
            }
            new Client_del();
            validate();
            return;
        }
        else if(str.equals("个人信息")){
            String sql;
            //判断是否为管理员
            if(!user.isFlag()){
                Worker_update worker_update = new Worker_update(user);
                user = worker_update.updateuser();
                return;
            }else {
                Admin_update admin_update = new Admin_update(user);
                admin_update.updateuser();
                return;
            }
        }else if(str.equals("货物类别")){
            UIsort uIsort = new UIsort();
            validate();
            return;
        }else if(str.equals("销售统计")){
            UIcash uIcash = new UIcash();
            validate();
            return;
        }else if(str.equals("注销")){
            Login login = new Login();
            this.dispose();
            validate();
            return;
        }


    }
}
